import 'package:flutter/material.dart';
import "package:flutter_screenutil/flutter_screenutil.dart";
import 'package:get/get.dart';

/* 时间范围 */
class DateTimeRanges extends StatelessWidget {
  final String startTime;
  final String endTime;
  final dynamic Function(String, String) onChanged;

  const DateTimeRanges(
      {this.startTime = '', this.endTime = '', required this.onChanged});

  @override
  Widget build(BuildContext context) {
    DateTimeRangesController vm = Get.put(DateTimeRangesController());
    vm.startTime.value = startTime;
    vm.endTime.value = endTime;
    return Container(
        height: 100.h,
        child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          Expanded(
              flex: 5,
              child: InkWell(
                  onTap: () async {
                    var result = await showDatePicker(
                        context: context,
                        initialDate: vm.startTime.value != ''
                            ? DateTime.parse(vm.startTime.value)
                            : DateTime.now(),
                        lastDate: DateTime.now(),
                        firstDate: DateTime(1997));
                    if (result != null) {
                      if (vm.startTime.value != '' &&
                          vm.endTime.value != '' &&
                          result.isAfter(DateTime.parse(vm.endTime.value))) {
                        vm.startTime.value = vm.endTime.value;
                        vm.endTime.value = result.toString();
                      } else {
                        vm.startTime.value = result.toString();
                      }
                      onChanged(vm.startTime.value, vm.endTime.value);
                    }
                  },
                  child: Obx(() => TextField(
                      controller:
                          TextEditingController(text: vm.startTime.value),
                      enabled: false,
                      style: TextStyle(
                        color: Color.fromRGBO(193, 192, 192, 1),
                        fontSize: 36.sp,
                        fontWeight: FontWeight.w400,
                      ),
                      decoration: InputDecoration(
                          contentPadding:
                              EdgeInsets.only(left: 20.sp),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                Radius.circular(10.sp),
                              ),
                              borderSide: BorderSide(
                                  width: 2.sp,
                                  color: Color.fromRGBO(204, 204, 204, 1))),
                          suffixIcon: Icon(Icons.date_range_outlined,
                              color: Color.fromRGBO(153, 153, 153, 1),
                              size: 50.sp),
                          hintText: '开始时间',
                          hintStyle: TextStyle(
                              color: Color.fromRGBO(153, 153, 153, 1),
                              fontSize: 36.sp,
                              fontWeight: FontWeight.w400)))))),
          Expanded(
              flex: 1,
              child: Center(
                  child: Text('─',
                      style: TextStyle(
                          color: Color.fromRGBO(153, 153, 153, 1),
                          fontSize: 40.sp,
                          fontWeight: FontWeight.w400)))),
          Expanded(
              flex: 5,
              child: InkWell(
                  onTap: () async {
                    var result = await showDatePicker(
                        context: context,
                        initialDate: vm.endTime.value != ''
                            ? DateTime.parse(vm.endTime.value)
                            : DateTime.now(),
                        lastDate: DateTime.now(),
                        firstDate: DateTime(1997));
                    if (result != null) {
                      if (vm.startTime.value != '' &&
                          vm.endTime.value != '' &&
                          result.isBefore(DateTime.parse(vm.startTime.value))) {
                        vm.endTime.value = vm.startTime.value;
                        vm.startTime.value = result.toString();
                      } else {
                        vm.endTime.value = result.toString();
                      }
                      onChanged(vm.startTime.value, vm.endTime.value);
                    }
                  },
                  child: Obx(() => TextField(
                      controller: TextEditingController(text: vm.endTime.value),
                      enabled: false,
                      style: TextStyle(
                        color: Color.fromRGBO(193, 192, 192, 1),
                        fontSize: 36.sp,
                        fontWeight: FontWeight.w400,
                      ),
                      decoration: InputDecoration(
                          contentPadding:
                              EdgeInsets.only(left: 20.sp),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                Radius.circular(10.sp),
                              ),
                              borderSide: BorderSide(
                                  width: 2.sp,
                                  color: Color.fromRGBO(204, 204, 204, 1))),
                          suffixIcon: Icon(Icons.date_range_outlined,
                              color: Color.fromRGBO(153, 153, 153, 1),
                              size: 50.sp),
                          hintText: '结束时间',
                          hintStyle: TextStyle(
                              color: Color.fromRGBO(153, 153, 153, 1),
                              fontSize: 36.sp,
                              fontWeight: FontWeight.w400)))))),
        ]));
  }
}

class DateTimeRangesController extends GetxController {
  final startTime = ''.obs;
  final endTime = ''.obs;
}
